package com.bingxue.edu.management.education.service;

import cn.hutool.core.convert.Convert;
import com.bingxue.edu.common.enums.ChangeTypeEnum;
import com.bingxue.edu.common.enums.OperateStatusEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.management.education.mapper.EnrollChangeRecordMapper;
import com.bingxue.edu.management.education.model.entity.ClassInfo;
import com.bingxue.edu.management.education.model.entity.EnrollChangeRecord;
import com.bingxue.edu.management.education.model.entity.Enrollment;
import com.bingxue.edu.management.education.model.query.EnrollChangeRecordQuery;
import com.bingxue.edu.management.education.model.req.ClassCancelReq;
import com.bingxue.edu.management.education.model.req.ClassChangeReq;
import com.bingxue.edu.management.education.model.resp.EnrollChangeRecordResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.common.enums.ChangeTypeEnum.CHANGE_CLASS;
import static com.bingxue.edu.common.enums.ChangeTypeEnum.CHANGE_LESSON;
import static com.bingxue.edu.management.education.model.entity.table.EnrollChangeRecordTableDef.ENROLL_CHANGE_RECORD;

/**
 * 学生报名变更业务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EnrollChangeRecordService extends BaseService<EnrollChangeRecordMapper, EnrollChangeRecord> {

    public Page<EnrollChangeRecordResp> queryPage(EnrollChangeRecordQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        return this.pageAs(pageQuery.toPage(), queryWrapper, EnrollChangeRecordResp.class);
    }

    /**
     * 变更信息查询
     * @param id  报名id
     */
    public List<EnrollChangeRecord> queryChange(String id) {
        return this.queryChain()
                .where(ENROLL_CHANGE_RECORD.ENROLLMENT_ID.eq(id))
                    .and(ENROLL_CHANGE_RECORD.OPERATE_STATUS.eq(OperateStatusEnum.SUCCESS))
                .orderBy(ENROLL_CHANGE_RECORD.CREATE_TIME.asc())
                .list();
    }

    /**
     * 是否存在退课记录
     * 保证同一时间只能存在一笔记录
     */
    public boolean existPendingCancelRecord(String enrollmentId) {
        return this.exists(ENROLL_CHANGE_RECORD.ENROLLMENT_ID.eq(enrollmentId)
                .and(ENROLL_CHANGE_RECORD.OPERATE_STATUS.in(
                        OperateStatusEnum.VERIFYING,
                        OperateStatusEnum.REFUNDING
                )).and(ENROLL_CHANGE_RECORD.CHANGE_TYPE.in(
                        ChangeTypeEnum.CANCEL_CLASS,
                        ChangeTypeEnum.CANCEL_LESSON
                )));
    }

    /**
     * 初始化变更记录
     */
    public EnrollChangeRecord initChangeRecord(ClassChangeReq req, Enrollment enrollment, ClassInfo oldClass, ClassInfo newClass, List<Integer> lessonIndexList) {
        String lessonIndexListStr = Convert.toStr(lessonIndexList);
        EnrollChangeRecord enrollChangeRecord = new EnrollChangeRecord();
        enrollChangeRecord.setEnrollmentId(enrollment.getId());
        enrollChangeRecord.setStudentId(req.getStudentId());
        enrollChangeRecord.setChangeType(req.getChangeType());
        enrollChangeRecord.setOriginalClassId(oldClass.getId());
        enrollChangeRecord.setOriginalLessonIndexes(lessonIndexList);
        enrollChangeRecord.setTargetClassId(newClass.getId());
        enrollChangeRecord.setTargetLessonIndexes(lessonIndexList);
        enrollChangeRecord.setOperateStatus(OperateStatusEnum.SUCCESS);
        String details = null;
        if (req.getChangeType() == CHANGE_CLASS) {
            details = "从[%s]转班至[%s]，转班课次为第%s次课，共%d次课".formatted(oldClass.getName(), newClass.getName(), lessonIndexListStr, lessonIndexList.size());
        } else if (req.getChangeType() == CHANGE_LESSON) {
            details = "从[%s]调课至[%s]，调课课次为第%s次课, 共%d次课".formatted(oldClass.getName(), newClass.getName(), lessonIndexListStr, lessonIndexList.size());
        }
        enrollChangeRecord.setDetails(details);
        enrollChangeRecord.setOperateSource(req.getOperateSource());
        return enrollChangeRecord;
    }

    /**
     * 初始化变更记录
     */
    public EnrollChangeRecord initCancelRecord(ClassCancelReq req, Enrollment enrollment, ClassInfo classInfo, List<Integer> lessonIndexList) {
        String lessonIndexListStr = Convert.toStr(lessonIndexList);
        EnrollChangeRecord enrollChangeRecord = new EnrollChangeRecord();
        enrollChangeRecord.setEnrollmentId(enrollment.getId());
        enrollChangeRecord.setStudentId(req.getStudentId());
        enrollChangeRecord.setChangeType(req.getChangeType());
        enrollChangeRecord.setOriginalClassId(classInfo.getId());
        enrollChangeRecord.setOriginalLessonIndexes(lessonIndexList);
        enrollChangeRecord.setOperateStatus(OperateStatusEnum.VERIFYING);
        String details = "从[%s]%s，课次为第%s次课，共%d次课".formatted(classInfo.getName(), req.getChangeType().getLabel(), lessonIndexListStr, lessonIndexList.size());
        enrollChangeRecord.setDetails(details);
        enrollChangeRecord.setOperateSource(req.getOperateSource());
        return enrollChangeRecord;
    }

}
